package com.wangyk.rabbitmqconsole.service.impl;

import com.wangyk.rabbitmqcommon.interceptor.Pagination;
import com.wangyk.rabbitmqcommon.model.console.User;
import com.wangyk.rabbitmqcommon.resp.CommonResp;
import com.wangyk.rabbitmqconsole.dao.UserMapper;
import com.wangyk.rabbitmqconsole.pojoProperties.RootInfo;
import com.wangyk.rabbitmqconsole.service.UserService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @Author: wangyk
 * @CreateDate: 2018/12/28$ 14:51$
 * @UpdateUser: wangyk
 * @UpdateDate: 2018/12/28$ 14:51$
 * @UpdateRemark:
 * @Version: 1.0
 */
@Service
public class UserServiceImpl implements UserService
{
    private Logger logger = LogManager.getLogger(UserServiceImpl.class);

    @Resource
    private UserMapper userMapper;

    @Value("${value.name}")
    private String NAME;

    @Value("${value.pass}")
    private String PASSWORD;

    @Value("${sql_log}")
    private String sqlLevel;

    @Autowired
    private RootInfo rootInfo;

    @Override
    @Transactional
    public User selectUserById(Integer userId)
    {
        User user = userMapper.selectByPrimaryKey(userId);
        return user;
    }

    @Override
    public List<User> selectAll()
    {
        logger.info("==========Service分界线==========");
        System.out.println(rootInfo.getName());
        System.out.println(sqlLevel);
        List<User> users = userMapper.selectAll();
        logger.info("==========Service分界线==========");
        return users;
    }

    @Override
    @Transactional
    public CommonResp deleteByPrimaryKey(Integer userId)
    {
        userMapper.deleteByPrimaryKey(userId);
        return null;
    }

    @Override
    @Transactional
    public CommonResp updateUserInfo(User user)
    {
        user.setUserId(1);
        user.setUserName("赵铁柱朱");
        user.setPassword("zuzuzu");
        user.setSex((byte)1);
        user.setDate(new Date());
        userMapper.updateByPrimaryKeySelective(user);
        return null;
    }

    @Override
    @Transactional//添加事务注解，遇到异常回滚。
    public CommonResp insertUserReturnId(User user)
    {
        user.setUserName("第11个测试者");
        user.setPassword("test");
        user.setSex((byte)99);
        user.setDate(new Date());
        userMapper.insertUserReturnId(user);
        int i=10/0;
        return null;
    }

    @Override
    public CommonResp selectUsersByPage()
    {
        CommonResp resp=new CommonResp();
        logger.debug(NAME+":"+PASSWORD);
        Pagination page=new Pagination();
        page.setPageNumber(5);
        page.setCurrentPage(1);
        String userName="";
        List<User> users=userMapper.selectUsersByPage(page,userName);
        resp.setResult(users);
        return resp;
    }

}
